﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MarkDAO
/// </summary>
public class MarkDAO : DBAccess, IMark
{
    #region sql commands
    private string SQL_GET_ALL = "select * from Marks";
    private string SQL_INSERT_MARK = "insert into Marks(Mark, Remarks, PostingID, StaffID) values(@mark, @remarks, @postingId, @staffId)";
    private string SQL_UPDATE_MARK = "update Marks set Mark = @mark, Remarks = @remarks, PostingID = @postingId, StaffID = @staffId where MarkID = @markId";
    private string SQL_DELETE_MARK = "delete from Marks where MarkID = @markId";
    string message;
    #endregion
    public MarkDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    #region IMark Members

    /// <summary>
    /// Get all marks
    /// </summary>
    /// <returns>IList</returns>
    public IList<MarkInfo> GetAllMarks()
    {

        IList<MarkInfo> markList = new List<MarkInfo>();
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_GET_ALL, conn);
            if (Connect())
            {
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MarkInfo mark = new MarkInfo();
                    mark.MarkId = reader.GetInt32(0);
                    mark.Mark = reader.GetString(1);
                    mark.Remarks = reader.GetString(2);
                    mark.PostingId = reader.GetInt32(3);
                    mark.StaffId = reader.GetString(4);

                    markList.Add(mark);
                }
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return markList;

    }

    /// <summary>
    /// Insert mark
    /// </summary>
    /// <param name="mark"></param>
    /// <returns>int</returns>
    public int InsertMark(MarkInfo mark)
    {

        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT_MARK, conn);
            cmd.Parameters.AddWithValue("@mark", mark.Mark);
            cmd.Parameters.AddWithValue("@remarks", mark.Remarks);
            cmd.Parameters.AddWithValue("@postingId", mark.PostingId);
            cmd.Parameters.AddWithValue("@staffId", mark.StaffId);

            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;

    }

    /// <summary>
    /// Update Mark
    /// </summary>
    /// <param name="mark"></param>
    /// <returns>int</returns>
    public int UpdateMark(MarkInfo mark)
    {

        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE_MARK, conn);
            cmd.Parameters.AddWithValue("@mark", mark.Mark);
            cmd.Parameters.AddWithValue("@remarks", mark.Remarks);
            cmd.Parameters.AddWithValue("@postingId", mark.PostingId);
            cmd.Parameters.AddWithValue("@staffId", mark.StaffId);
            cmd.Parameters.AddWithValue("@markId", mark.MarkId);


            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;

    }


    /// <summary>
    /// Delete Mark
    /// </summary>
    /// <param name="markId"></param>
    /// <returns>int</returns>
    public int DeleteMark(int markId)
    {

        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_DELETE_MARK, conn);
            cmd.Parameters.AddWithValue("@markId", markId);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;

    }

    #endregion
}